#!/bin/sh
### BEGIN INIT INFO
# Provides:          cloudserver
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     true
# Short-Description: Start/stop cloudserver server
### END INIT INFO
 
case $1 in
    start)
        echo "Starting cloudserver ..."
        if [ ! -f /usr/local/cloudserver/pid ]; then
            nohup java -jar /usr/local/cloudserver/cloudserver.jar /usr/local/cloudserver 2>> /dev/null >> /dev/null &
            echo $! > /usr/local/cloudserver/pid
            echo "cloudserver started ..."
        else
            echo "cloudserver is already running ..."
        fi
    ;;
    stop)
        if [ -f /usr/local/cloudserver/pid ]; then
            PID=$(cat /usr/local/cloudserver/pid);
            echo "Stopping cloudserver ..."
            kill $PID;
            echo "cloudserver stopped ..."
            rm /usr/local/cloudserver/pid
        else
            echo "cloudserver is not running ..."
        fi
    ;;
    restart)
        if [ -f /usr/local/cloudserver/pid ]; then
            PID=$(cat /usr/local/cloudserver/pid);
            echo "Stopping cloudserver ...";
            kill $PID;
            echo "cloudserver stopped ...";
            rm /usr/local/cloudserver/pid
 
            echo "Starting cloudserver ..."
            nohup java -jar /usr/local/cloudserver/cloudserver.jar /usr/local/cloudserver 2>> /dev/null >> /dev/null &
            echo $! > /usr/local/cloudserver/pid
            echo "cloudserver started ..."
        else
            echo "cloudserver is not running ..."
        fi
    ;;
esac
